package com.jxs.android.weixi;

import java.io.IOException;
import java.util.TreeMap;

import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.xmlpull.v1.XmlPullParserException;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.jxs.android.soap.BuWebService;
import com.jxs.android.soap.IWebService;
import com.jxs.android.soap.SoapUtil;
import com.jxs.android.util.HandlerHolder;
import com.jxs.android.weixi.data.CustBaseInfoNew;
import com.telecom.weixi.WeixiApplication;

public class Search_GetCustBaseInfoNewHolder {

	private Handler handler;
	private int pageIndex = -1;

	public Search_GetCustBaseInfoNewHolder(Handler handler, int pageIndex) {
		this.handler = handler;
		this.pageIndex = pageIndex;
	}

	public void getCustBaseInfoNew(final String ticket, final Object... params) {
		new Thread(new Runnable() {
			@Override
			public void run() {
				String wsdl_url = WeixiApplication.WSDL_URL;// wsdl
				String namespace = WeixiApplication.WSDL_NAME_SPACE;// namespace
				String methodName = "getCustBaseInfoNew";// 要锟斤拷锟矫的凤拷锟斤拷锟斤拷锟�
				SoapObject request = new SoapObject(namespace, methodName);
				request.addProperty("ticket", ticket);
				request.addProperty("cust_id", params[0].toString());
				request.addProperty("serv_id", params[1].toString());
				BuWebService webService = new BuWebService(wsdl_url, request);
				webService.setDoNet(false);
				webService.requestWSDL(new IWebService() {
					@Override
					public void logic(SoapSerializationEnvelope envelope) throws IOException, XmlPullParserException {
						SoapObject response = (SoapObject) envelope.getResponse();
						paser(response, params);
					}

				});
			}
		}).start();
	}

	public void paser(SoapObject soapObject, Object... params) {
		if (null != soapObject) {
			String stauts = soapObject.getProperty("status").toString();
			SoapObject msgObjec = (SoapObject) soapObject.getProperty("msg");
			String message = msgObjec.getProperty("message").toString();
			if ("-2".equals(stauts)) {
				Message msg = new Message();
				msg.what = HandlerHolder.What.ERROR_MSG;
				msg.obj = message;
				handler.sendMessage(msg);
			} else if ("-1".equals(stauts)) {
				Message msg = new Message();
				msg.what = HandlerHolder.What.TO_LOGIN;
				msg.obj = message;
				handler.sendMessage(msg);
			} else if ("0".equals(stauts)) {
				SoapObject taskBeanObject = (SoapObject) soapObject.getProperty("retMsg");
				Message msg = new Message();
				msg.what = HandlerHolder.What.UPDATE_LIST_UI;
				msg.arg1 = pageIndex;
				msg.setData(new Bundle());
				msg.obj = paserData(taskBeanObject, params);
				handler.sendMessage(msg);
			}
		}
	}

	private Object paserData(SoapObject retMsgObject, Object... params) {
		TreeMap<String, CustBaseInfoNew> tems = new TreeMap<String, CustBaseInfoNew>();
		if (null != retMsgObject) {
			for (int i = 0; i < retMsgObject.getPropertyCount(); i++) {
				SoapObject taskBeanObject = (SoapObject) retMsgObject.getProperty(i);
				CustBaseInfoNew cstInfo = new CustBaseInfoNew();
				cstInfo.pos = SoapUtil.getValue(taskBeanObject, "pos");
				cstInfo.title = SoapUtil.getValue(taskBeanObject, "title");
				cstInfo.content = SoapUtil.getValue(taskBeanObject, "content");
				tems.put(cstInfo.pos, cstInfo);
			}
		}
		return tems;
	}
}
